<?php
namespace Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Predicate\Like;
use Zend\Db\Sql\Predicate\Operator;
use Zend\Db\Sql\Select;
use Zend\InputFilter\InputFilter;

class UnitTable extends AbstractTable implements LanguageAwareInterface
{

    /**
     * @var string
     */
    private $_lang;

    /**
     * @param $id
     * @return Unit
     */
    public function getUnit($id){
       return $this->getById($id);
    }

    /**
     * @return InputFilter
     */
    public function getInputFilter()
    {
        $filter = new InputFilter();
        return $filter;
    }


    public function getLang()
    {
        return $this->_lang;
    }

    public function setLang($lang)
    {
        $this->_lang = $lang;
    }

    public function getAllAsArray(){
        $select = $this->getDbSqlSelect();
        $select->join(array('ul'=>'unit_lang'),'ul.unit_id = unit.id', array('name'=>'name'))
        ->where(array('ul.lang'=>$this->getLang()));
        $resultSet = $this->getTableGateway()->selectWith($select);
        $returnArr = array();
        foreach ($resultSet as $item) {
            $returnArr[] =  $item->toArray();
        }
        return $returnArr;
    }

    /**
     * @param $name
     * @return Unit
     */
    public function getUnitByName($name){
        $select = $this->getDbSqlSelect();
        $select->join(array('ul'=>'unit_lang'),'ul.unit_id = unit.id', array('name'=>'name'))
            ->where(array('ul.lang'=>$this->getLang(), 'name'=>$name));
        $resultSet = $this->getTableGateway()->selectWith($select);
        foreach ($resultSet as $aUnit) {
            return $aUnit;
        }

    }



}